package com.bolt.report.engine.model;

import com.bolt.common.collection.IteratorUtil;
import com.bolt.common.convert.Convert;
import com.bolt.common.utils.NumberUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.report.definition.SortOrder;
import com.bolt.report.engine.BRGroupField;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class GroupTree implements Comparable<GroupTree> {

    private GroupItem groupItem;

    private int depth;

    private BRGroupField groupField;

    private List<GroupTree> children;

    public GroupTree(BRGroupField groupField) {
        this.groupField = groupField;
    }

    public GroupItem getGroupItem() {
        return groupItem;
    }

    public void setGroupItem(GroupItem groupItem) {
        this.groupItem = groupItem;
    }

    public List<GroupTree> getChildren() {
        return children;
    }

    public void setChildren(List<GroupTree> children) {
        this.children = children;
    }


    public int getDepth() {
        return depth;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    public BRGroupField getGroupField() {
        return groupField;
    }

    public int getSize() {
        if (IteratorUtil.isNotEmpty(this.children)) {
            return this.children.size();
        } else {
            return 0;
        }
    }

    public int getDataSize() {
        DataRange dataRange = this.groupItem.getRange();
        if (IteratorUtil.isNotEmpty(dataRange.getBindData())) {
            return dataRange.getBindData().size();
        } else {
            return 0;
        }
    }


    public void setGroupField(BRGroupField groupField) {
        this.groupField = groupField;
    }

    private int compare(GroupTree o1, GroupTree o2) {
        Object data1 = o1.getGroupItem().getValue();
        Object data2 = o2.getGroupItem().getValue();
        if (data1 == null || data2 == null) {
            return 1;
        }
        if (data1 instanceof Date) {
            Date d1 = (Date) data1;
            Date d2 = (Date) data2;
            if (SortOrder.ASC.equals(groupField.getSortOrder().ASC)) {
                return d1.compareTo(d2);
            } else {
                return d2.compareTo(d1);
            }
        } else if (data1 instanceof Number || NumberUtil.isNumber(data1.toString())) {
            BigDecimal n1 = Convert.toBigDecimal(data1);
            BigDecimal n2 = Convert.toBigDecimal(data2);
            if (SortOrder.ASC.equals(groupField.getSortOrder().ASC)) {
                return n1.compareTo(n2);
            } else {
                return n2.compareTo(n1);
            }
        } else {
            String str1 = data1.toString();
            String str2 = data2.toString();
            if (SortOrder.ASC.equals(groupField.getSortOrder().ASC)) {
                return StrUtil.compare(str1, str2, true);
            } else {
                return StrUtil.compare(str2, str1, true);
            }
        }
    }


    @Override
    public int compareTo( GroupTree o) {
        return compare(this, o);
    }


}
